<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
      v-hasPermi="['fang:maintenance:list']"
    >
      <el-form-item label="维修编号" prop="maintenanceNumber">
        <el-input
          v-model="queryParams.maintenanceNumber"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户姓名" prop="tenantName">
        <el-input
          v-model="queryParams.tenantName"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="维修状态" prop="maintenanceStatus">
        <el-select
          v-model="queryParams.maintenanceStatus"
          placeholder="维修状态"
          clearable
          style="width: 240px"
        >
          <el-option
            label="待处理"
            :value="MAINTENANCE_STATUS.PENDING"
          ></el-option>
          <el-option
            label="维修中"
            :value="MAINTENANCE_STATUS.REPAIR"
          ></el-option>
          <el-option
            label="待确认"
            :value="MAINTENANCE_STATUS.DETERMINED"
          ></el-option>
          <el-option
            label="维修完成"
            :value="MAINTENANCE_STATUS.COMPLETED"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['fang:maintenance:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fang:maintenance:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['fang:maintenance:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="maintenanceList"
      @selection-change="handleSelectionChange"
      v-hasPermi="['fang:maintenance:list']"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="自增ID" align="center" prop="id" /> -->
      <!-- <el-table-column label="租户ID" align="center" prop="tenantUserId" /> -->

      <el-table-column
        label="维修编号"
        align="center"
        prop="maintenanceNumber"
      />

      <el-table-column label="联系方式" align="center" prop="tenantUserPhone" />
      <el-table-column label="租户名称" align="center" prop="tenantUserName" />

      <el-table-column
        label="保修内容"
        align="center"
        prop="simpleDescription"
      />

      <el-table-column
        label="报修时间"
        align="center"
        prop="repairCreateTime"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.repairCreateTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="维修状态"
        align="center"
        prop="maintenanceStatus"
        :formatter="maintenanceStatusText"
      />

      <!-- 
      1：待处理、2：维修中、3:维修完成
      <el-table-column label="租户房屋ID" align="center" prop="houseId" /> 
      <el-table-column label="详细描述" align="center" prop="detailDescription" />
      <el-table-column label="图片，支持5张" align="center" prop="descriptionPic" />
       -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleDetial(scope.row)"
            v-hasPermi="['fang:maintenance:query']"
            >详情</el-button
          >
          <el-button
            link
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fang:maintenance:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fang:maintenance:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
      v-hasPermi="['fang:maintenance:list']"
    />

    <maintenanceAdd ref="childAddRef" @refresh="getList"></maintenanceAdd>
    <maintenanceDetail ref="childDetailRef"></maintenanceDetail>
  </div>
</template>

<script setup name="Maintenance">
import {
  listMaintenance,
  getMaintenance,
  delMaintenance,
  addMaintenance,
  updateMaintenance,
} from "@/api/fang/maintenance";
import maintenanceAdd from "./add.vue";
import maintenanceDetail from "./detail.vue";
import { MAINTENANCE_STATUS, MAINTENANCE_STATUS_TEXT } from "@/utils/enums";

const { proxy } = getCurrentInstance();

const maintenanceList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const childAddRef = ref(null);
const childDetailRef = ref(null);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    maintenanceNumber: null,
    tenantName: null,
    maintenanceStatus: null,
  },
});

const { queryParams } = toRefs(data);

function maintenanceStatusText(row, column, cellValue) {
  return MAINTENANCE_STATUS_TEXT[cellValue] || "未知状态";
}

/** 查询维保列表 */
function getList() {
  loading.value = true;
  listMaintenance(queryParams.value).then((response) => {
    maintenanceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  childAddRef.value.openAddDialog();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  // 修改功能通过详情页面实现，跳转到详情页面
  const _id = row.id || ids.value;
  childDetailRef.value.openDetailDialog(_id);
}

/** 按钮操作 */
function handleDetial(row) {
  const _id = row.id || ids.value;
  childDetailRef.value.openDetailDialog(_id);
  // reset()
  // const _id = row.id || ids.value
  // getMaintenance(_id).then(response => {
  //   form.value = response.data
  //   open.value = true
  //   title.value = "修改维保"
  // })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除维保编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delMaintenance(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "fang/maintenance/export",
    {
      ...queryParams.value,
    },
    `maintenance_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
